import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
import os

def fingerMatch(one,two):
    # 读取指纹图像 
    img1 = cv2.imread(one, 0)
    # img2 = cv2.imread('zwy/3-1.jpg', 0)
    img2 = cv2.imread(two, 0)

    # 对图像进行预处理，如去噪、增强等
    # 这里可以使用各种图像处理技术，如高斯滤波、直方图均衡化等

    # 使用结构相似性指数（SSIM）进行图像比对
    similarity_index, _ = ssim(img1, img2, full=True)

    # 设置阈值，根据相似性指数判断是否匹配
    threshold = 0.1
    if similarity_index > threshold:
        print("指纹匹配")
        return True
    else:
        print("指纹不匹配")
        return False

tmpimage='tmp/fingerprint_image.bmp'
def anymatch():
    for filename in os.listdir('data'):
        # 构建文件的完整路径
        file_path = os.path.join('data', filename) 
        # print(file_path)
        ismatch = fingerMatch(tmpimage,file_path)
        if(ismatch):
            return file_path
        
# 转换图片
def changeImg(bmp_image):
    if bmp_image is not None:
        # 将BMP图像保存为JPG格式
        cv2.imwrite(bmp_image.split(',')[0]+'.jpg', bmp_image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
        print("BMP图像已成功转换为JPG格式并保存为output_image.jpg。")
    else:
        print("无法读取BMP图像。请检查文件路径或图像是否损坏。")
        
if __name__=='__main__':
    # changeImg(tmpimage)
    fingerMatch('zwy/3.jpg','zwy/3-1.jpg')
    str=anymatch()
    print(str)